// 使用方式
// <view v-if="dataList&&dataList.length > 0">
// 	<view v-for=""></view>
// 	<view class="o-ptb">
// 		<u-loadmore font-size="20" :status="status" :load-text="loadText" />
// 	</view>
// </view>
// <view v-else>
// 	<empty></empty>
// </view>
// 混入
import Config from './config.js'
export default {
    data() {
        return {
            initial: {
                createdIsNeed: true, // 此页面是否在创建时，调用查询数据列表接口？
                geturl: '', // API地址
                getDataListIsPage: true, // 是否分页
                method: 'GET' // 请求方式
            },
            status: 'loadmore', //loadmore:上滑加载更多  loading:加载中 nomore:我是有底线的
            timer: {
                pull: null,
                reachBottom: null,
            },
			dataForm: {}, //传参
            dataList: [], // 请求到的数据
            current: 1, // 页码
            size: 10,
            total: '', // 总条数
            // sort: '', // 排序字段
            // sortMode: '' // 排序模式
            loadText: {
                loadmore: '上拉加载',
                loading: '努力加载中',
                nomore: '实在没有了'
            },
            // allSelect: false,
        }
    },
	onShow() {
		// 使用方法: uni.$emit('updateData', true)
		uni.$on('updateData',(data) => {
			console.log('监听到事件来自 updateData ，携带参数为：' + data);
			if(data) {
				this.getDataList()
			}
		})
	},
    created() {
        if (this.initial.createdIsNeed) {
            this.getDataList()
        }
    },
    methods: {
        clearTimer() {
            for (let key in this.timer) {
                clearTimeout(this.timer[key])
                this.timer[key] = null
            }
        },
        getDataList(callback) {
            this.current = 1
            this.status = "loadmore"
            this.getData(callback)
        },
        // 获取数据
        getData(callback, hideLoading) {
            if (hideLoading) {
                this.status = "loading"
            }
            uni.request({
                url: Config.url_config + this.initial.geturl, //请求路径
                method: this.initial.method,
                hideLoading: hideLoading,
                header: {
                    'Blade-Auth': 'bearer ' + uni.getStorageSync('token'),
                    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
                },
                data: {
                    current: this.initial.getDataListIsPage ? this.current : null,
                    size: this.initial.getDataListIsPage ? this.size : null,
                    ...this.dataForm
                },
            })
            .then((res) => {
				// console.log('res ==>', res)
				let opt = res[1].data
				if(opt.code == 200) {
					let data = opt.data
					if (this.initial.getDataListIsPage == true && this.current !== 1) {
						this.dataList = data.records ? this.dataList.concat(data.records) : this.dataList.concat(data)
					} else {
						this.dataList = data.records ? data.records : data
					}
					this.total = data.total
					if (callback) {
						this.$nextTick(() => {
							callback()
						})
					}
				}else if(opt.code == 401) {
					uni.clearStorageSync();
					this.$Function.Toast(opt.msg)
					this.$Function.LinkTo('/pages/login/index','Rl')
					// #ifdef MP-WEIXIN
					// this.$Function.LinkTo('/pages/home/index','Rl')
					// #endif
					// #ifdef APP-PLUS || H5
					// #endif
				}
				
			})
			.catch((err) => {
				console.log('err ==>', err)
			}).finally(() => {
                if (this.dataList.length) {
                    this.dataList.length == this.total ? this.status = "nomore" : this.status = 'loadmore'
                } else {
                    this.status = "noData"
                }
            })
        }
    },
    //下拉刷新
    onPullDownRefresh() {
        this.dataList = []
        this.timer.pull = setTimeout(() => {
            this.getDataList()
            uni.stopPullDownRefresh();
        }, 1000)
    },
    //上拉加载
    onReachBottom() {
        if (this.total === this.dataList.length) {
            this.dataList.length ? this.status = "nomore" : this.status = "noData"
        } else {
            this.status = "loading"
            let hideLoading = true
            this.timer.reachBottom = setTimeout(() => {
                this.current++
                this.getData(null, hideLoading)
            }, 1000)
        }
    },

    onHide() {
        this.clearTimer()
    },
    onUnload() {
        this.clearTimer()
    },
    destroyed() {
        this.clearTimer()
    }
}